﻿using System;
using System.Collections.Generic;
using Jacobi.Seesmic.FeedReader.FeedItemView;
using Jacobi.Seesmic.FeedReader.Model;
using Seesmic.Sdp.Extensibility;
using Seesmic.Sdp.Utils;

namespace Jacobi.Seesmic.FeedReader
{
    /// <summary>
    /// Manages the sidebar action that is coupled to this timeline.
    /// </summary>
    public sealed class FeedTimeline : Timeline
    {
        /// <summary>
        /// Constructs a new instance.
        /// </summary>
        private FeedTimeline()
        {
            SidebarAction = new ShowTimelineAction(this);
        }

        /// <summary>Singleton instance.</summary>
        private static FeedTimeline _instance = new FeedTimeline();

        /// <summary>
        /// The one and only Timeline this plugin shows.
        /// </summary>
        internal static FeedTimeline Current
        { get { return _instance; } }

        /// <summary>The sidebar action to show this timeline.</summary>
        internal ShowTimelineAction SidebarAction { get; private set; }

        /// <summary>
        /// Removes all <see cref="ITimelineItem"/>s owned by the <paramref name="feedManager"/> from the timeline.
        /// </summary>
        /// <param name="feedManager">The <see cref="FeedManager"/> to remove its items from the timeline.</param>
        /// <param name="callback">An optional delegate that receives all items to be removed. Can be null.</param>
        internal void RemoveItems(FeedManager feedManager, Action<TimelineItemContainer> callback)
        {
            var removedItems = new List<TimelineItemContainer>();

            foreach (TimelineItemContainer timelineContainer in Items)
            {
                var vm = timelineContainer.TimelineItem as FeedItemViewModel;

                if (vm != null && vm.Tag == feedManager)
                {
                    removedItems.Add(timelineContainer);
                }
            }

            foreach (var item in removedItems)
            {
                Remove(item);

                if (callback != null)
                {
                    callback(item);
                }
            }
        }

        /// <summary>
        /// Removes a single item from the timeline based on the specified <paramref name="itemId"/>.
        /// </summary>
        /// <param name="itemId">The unique Id of the timeline item.</param>
        /// <param name="callback">An optional delegate that receives the item to be removed. Can be null.</param>
        internal void Remove(string itemId, Action<TimelineItemContainer> callback)
        {
            foreach (TimelineItemContainer timelineContainer in Items)
            {
                var vm = timelineContainer.TimelineItem as FeedItemViewModel;

                if (vm != null && vm.Id == itemId)
                {
                    this.Remove(timelineContainer);

                    if (callback != null)
                    {
                        callback(timelineContainer);
                    }

                    break;
                }
            }
        }
    }
}